import requests

from common.headers import HeaderToken
from common.log_util import init_logger
from config import BASE_URL

logger = init_logger()
class EmployeeApi(object):
    path = "/api/sys/user"

    @classmethod  # 添加员工
    def add_user(cls, HeadersToken, body):
        add_url = BASE_URL + cls.path
        logger.debug(f'请求url:{add_url}')
        logger.debug(f'请求头:{HeadersToken}')
        logger.debug(f'请求体:{body}')
        resp = requests.post(add_url, headers=HeadersToken, json=body)
        logger.debug(f'响应体:{resp.json()}')
        return resp

    @classmethod  # 查询员工
    def query_emp(cls, headers, id):
        url = BASE_URL + cls.path + "/" + id
        resp = requests.get(url, headers=headers)
        return resp

    @classmethod  # 修改员工
    def modify_emp(cls, headers, id, body):
        url = BASE_URL + cls.path + "/" + id
        resp = requests.put(url, headers=headers, json=body)
        return resp

    @classmethod  # 删除员工
    def delete_emp(cls, headers):
        url = BASE_URL + cls.path + "/" + id
        resp = requests.delete(url, headers=headers)
        return resp


if __name__ == '__main__':
    # 新增员工
    # headers = {"Content-Type": "application/json"
    #     , "Authorization": "Bearer 8df2e240-ce82-4400-aca0-bfa73c2359a3"}
    body = {"username": "tom01",
            "mobile": "15112345602",
            "workNumber": "1"}
    headers = HeaderToken()
    resp = EmployeeApi.add_user(headers, body)
    print(resp.json())
    id = resp.json().get("data").get("id")

    # 查询员工
    l = EmployeeApi()
    print(l.query_emp(headers=headers, id=id).json())

    # 修改员工
    body = {"username": "tom-new",
            "password": "123456",
            "departmentId": "1066240656856453120"}
    resp = EmployeeApi.modify_emp(headers, id, body)
    print("修改员工：", resp.json())

    # 删除员工
    print(EmployeeApi.delete_emp(headers=headers).json())
